import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from './store/user'
import { ElMessage } from 'element-plus'

import Login from './views/login.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('./views/Home.vue'),
    children: [
      // 数据看板
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('./views/Dashboard.vue'),
        meta: { title: '数据看板' }
      },
      // 用户管理
      {
        path: 'users',
        children: [
          {
            path: 'admin-list',
            component: () => import('./views/users/list.vue'),
            meta: { 
              title: '后台用户列表',
              permission: ['admin:all', 'user:view', 'admin:manage']
            }
          },
          {
            path: 'app-list',
            component: () => import('./views/users/app-list.vue'),
            meta: { 
              title: '小程序用户列表',
              permission: ['admin:all', 'user:view', 'admin:manage']
            }
          },
          {
            path: 'profile',
            component: () => import('./views/users/profile.vue'),
            meta: { title: '个人信息' }
          },
        ]
      },
      // 公司相关
      {
        path: 'company',
        children: [
          // 广告管理
          {
            path: 'ad',
            children: [
              {path: 'list', component: () => import('./views/company/ad/list.vue'), meta: { title: '广告列表' }},
              {path: 'upload', component: () => import('./views/company/ad/upload.vue'), meta: { title: '广告上传' }},
            ]
          },
          // 库存管理
          {
            path: 'inventory',
            children: [
              {
                path: 'list', 
                component: () => import('./views/company/inventory/list.vue'),
                meta: { 
                  title: '库存列表',
                  permission: ['admin:all', 'product:all', 'product:edit']
                }
              },
              {
                path: 'publish', 
                component: () => import('./views/company/inventory/publish.vue'),
                meta: { 
                  title: '发布库存',
                  permission: ['admin:all', 'product:all', 'product:edit']
                }
              },
            ]
          },
          // 员工管理
          {
            path: 'employees',
            component: () => import('./views/company/employees.vue'),
            meta: { 
              title: '员工管理',
              permission: ['admin:all', 'admin:manage', 'employee:manage']
            }
          }
        ]
      },
      // 小程序管理
      {
        path: 'miniapp',
        children: [
          {
            path: 'banners',
            component: () => import('./views/miniapp/banners.vue'),
            meta: { 
              title: '轮播图管理',
              permission: ['admin:all', 'admin:manage']
            }
          },
          {
            path: 'data-upload',
            component: () => import('./views/miniapp/data-upload.vue'),
            meta: { 
              title: '数据上传',
              permission: ['admin:all', 'admin:manage']
            }
          },
          {
            path: 'file-upload',
            component: () => import('./views/miniapp/file-upload.vue'),
            meta: { 
              title: '文件上传',
              permission: ['admin:all', 'admin:manage']
            }
          }
        ]
      },
      // 社区管理
      {
        path: 'community',
        children: [
          {
            path: 'posts',
            component: () => import('./views/community/posts.vue'),
            meta: { 
              title: '帖子管理',
              permission: ['admin:all', 'community:manage']
            }
          },
          {
            path: 'comments',
            component: () => import('./views/community/comments.vue'),
            meta: { 
              title: '评论管理',
              permission: ['admin:all', 'community:manage']
            }
          }
        ]
      },
      // 教学管理
      {
        path: 'teaching',
        children: [
          {
            path: 'school',
            component: () => import('./views/teaching/school.vue'),
            meta: { 
              title: '学校信息',
              permission: ['admin:all', 'course:manage']
            }
          },
          {
            path: 'students',
            component: () => import('./views/teaching/students.vue'),
            meta: { 
              title: '学生信息',
              permission: ['admin:all', 'course:manage']
            }
          }
        ]
      },
      // 审核管理
      {
        path: 'audit',
        children: [
          {
            path: 'inventory',
            component: () => import('./views/audit/inventory.vue'),
            meta: { 
              title: '库存审核',
              permission: ['admin:all', 'product:all']
            }
          },
          {
            path: 'students',
            component: () => import('./views/audit/students.vue'),
            meta: { 
              title: '学生审核',
              permission: ['admin:all', 'course:manage']
            }
          },
          {
            path: 'users',
            component: () => import('./views/audit/users.vue'),
            meta: { 
              title: '后台用户审核',
              permission: ['admin:all', 'user:edit']
            }
          },
          {
            path: 'posts',
            component: () => import('./views/audit/posts.vue'),
            meta: { 
              title: '帖子审核',
              permission: ['admin:all', 'community:manage']
            }
          },
          {
            path: 'comments',
            component: () => import('./views/audit/comments.vue'),
            meta: { 
              title: '评论审核',
              permission: ['admin:all', 'community:manage']
            }
          }
        ]
      },
      // 系统设置
      {
        path: 'settings',
        component: () => import('./views/settings.vue'),
        meta: { title: '系统设置' }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('./views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 检查token是否过期
  userStore.checkTokenExpiration()

  // 不需要登录的路由
  if (to.path === '/login') {
    if (userStore.isLoggedIn) {
      next('/home/dashboard')
    } else {
      next()
    }
    return
  }

  // 需要登录的路由
  if (!userStore.isLoggedIn) {
    next('/login')
    return
  }

  // 权限验证
  if (to.meta.permission) {
    const hasPermission = to.meta.permission.some(perm => userStore.hasPermission(perm))
    if (!hasPermission) {
      ElMessage.error('没有权限访问该页面')
      next(from.path)
      return
    }
  }

  next()
})

export default router